package com.longdaji.decoration.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.longdaji.decoration.R;

/**
 * Created by longtao on 2018/2/7.
 */

public class ProcessView extends View {
    private int mWidth;
    private int mHeight;
    private int mStrokeColor;
    private int mStrokeWidth;
    private int mSolidColor;
    private int mInnerRadius;
    private int mOutterRadius;
    private Paint mPaint;
    private Context mContext;

    public ProcessView(Context context) {
        this(context, null);
    }

    public ProcessView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProcessView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProcessView);
        mInnerRadius = typedArray.getDimensionPixelSize(R.styleable.ProcessView_innerRadius, 0);
        mOutterRadius = typedArray.getDimensionPixelSize(R.styleable.ProcessView_outerRadius, 0);
        mSolidColor = typedArray.getColor(R.styleable.ProcessView_solidColor, getResources().getColor(R.color.black));
        mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.ProcessView_stroke, 0);
        mStrokeColor = typedArray.getColor(R.styleable.ProcessView_strokeColor, getResources().getColor(R.color.red));
        mContext = context;
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mSolidColor);
        canvas.drawCircle(mWidth / 2, mHeight / 2, mInnerRadius, mPaint);
        mPaint.setColor(mStrokeColor);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        canvas.drawCircle(mWidth/ 2, mHeight / 2, mOutterRadius, mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = heightSize;
        }
        setMeasuredDimension(mWidth, mHeight);
    }
}